package com.example.chat.controller;

import com.example.chat.config.WebSocketServer;
import com.example.chat.domain.Group;
import com.example.chat.domain.GroupMember;
import com.example.chat.domain.GroupMessage;
import com.example.chat.domain.MessageDTO;
import com.example.chat.service.GroupService;
import com.example.common.core.controller.BaseController;
import com.example.common.core.domain.R;
import com.example.common.core.enums.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 群组控制器
 */
@RestController
@RequestMapping("/groups")
public class GroupController extends BaseController {
    
    @Autowired
    private GroupService groupService;

    @RequestMapping("/push/{toUserId}")
    public ResponseEntity<String> pushToWeb(String message, @PathVariable String toUserId) throws IOException {
        WebSocketServer.sendInfo(message,toUserId);
        return ResponseEntity.ok("MSG SEND SUCCESS");
    }
    
    /**
     * 创建群组
     */
    @PostMapping
    public R<Long> createGroup(@RequestBody Group group) {
        Long groupId = groupService.createGroup(group);
        return R.ok(groupId);
    }
    
    /**
     * 获取群组信息
     */
    @GetMapping("/{groupId}")
    public R<Group> getGroup(@PathVariable Long groupId) {
        Group group = groupService.getGroup(groupId);
        return group != null ? R.ok(group) : R.fail(ResultCode.FAILED_NOT_EXISTS);
    }
    
    /**
     * 更新群组信息
     */
    @PutMapping("/{groupId}")
    public R<Boolean> updateGroup(@PathVariable Long groupId, @RequestBody Group group) {
        group.setGroupId(groupId);
        boolean result = groupService.updateGroup(group);
        return result ? R.ok(true) : R.fail(ResultCode.FAILED);
    }
    
    /**
     * 解散群组
     */
    @DeleteMapping("/{groupId}")
    public R<Boolean> dissolveGroup(@PathVariable Long groupId, @RequestParam Long userId) {
        boolean result = groupService.dissolveGroup(groupId, userId);
        return result ? R.ok(true) : R.fail(ResultCode.FAILED);
    }
    
    /**
     * 获取群组成员
     */
    @GetMapping("/{groupId}/members")
    public R<List<GroupMember>> getGroupMembers(@PathVariable Long groupId) {
        List<GroupMember> members = groupService.getGroupMembers(groupId);
        return R.ok(members);
    }
    
    /**
     * 添加群组成员
     */
    @PostMapping("/{groupId}/members")
    public R<Boolean> addGroupMember(@PathVariable Long groupId, @RequestBody GroupMember member) {
        member.setGroupId(groupId);
        boolean result = groupService.addGroupMember(member);
        return result ? R.ok(true) : R.fail(ResultCode.FAILED);
    }
    
    /**
     * 移除群组成员
     */
    @DeleteMapping("/{groupId}/members/{userId}")
    public R<Boolean> removeGroupMember(
            @PathVariable Long groupId,
            @PathVariable Long userId,
            @RequestParam Long operatorId) {
        boolean result = groupService.removeGroupMember(groupId, userId, operatorId);
        return result ? R.ok(true) : R.fail(ResultCode.FAILED);
    }
    
    /**
     * 退出群组
     */
    @PostMapping("/{groupId}/quit")
    public R<Boolean> quitGroup(@PathVariable Long groupId, @RequestParam Long userId) {
        boolean result = groupService.quitGroup(groupId, userId);
        return result ? R.ok(true) : R.fail(ResultCode.FAILED);
    }
    
    /**
     * 发送群消息
     */
    @PostMapping("/send")
    public R<GroupMessage> sendMessage(@RequestBody MessageDTO messageDTO) {
        GroupMessage message = groupService.saveGroupMessage(messageDTO);
        return message != null ? R.ok(message) : R.fail(ResultCode.FAILED);
    }
    
    /**
     * 获取群组消息历史
     */
    @GetMapping("/{groupId}/messages")
    public R<List<GroupMessage>> getGroupMessages(
            @PathVariable Long groupId,
            @RequestParam(defaultValue = "0") Integer offset,
            @RequestParam(defaultValue = "100") Integer limit) {
        List<GroupMessage> messages = groupService.getGroupMessages(groupId, offset, limit);
        return R.ok(messages);
    }
    
    /**
     * 撤回消息
     */
    @DeleteMapping("/messages/{messageId}")
    public R<Boolean> recallMessage(@PathVariable Long messageId, @RequestParam Long userId) {
        boolean result = groupService.recallMessage(messageId, userId);
        return result ? R.ok(true) : R.fail(ResultCode.FAILED);
    }
} 